﻿using randlib.core;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace Roll
{
    public class Roll32
    {
        [DllImport("rand_x64.dll", EntryPoint = "Roll32")]
        public unsafe extern static void Roll32_C(UInt32 seed, byte[] byteptr);


        [DllImport("rand_x64_n.dll", EntryPoint = "Roll32")]
        public unsafe extern static void Roll32_C_n(UInt32 seed, byte[] byteptr);

        [DllImport("rand_x64_n.dll", EntryPoint = "Seed")]
        public unsafe extern static void Seed_C_n(UInt32 seed);


        [DllImport("rand_x64_n.dll", EntryPoint = "Roll")]
        public unsafe extern static UInt32 Roll_C_n();


        [DllImport("go_64.dll", EntryPoint = "roll32")]
        public unsafe extern static void Roll32_Go(UInt32 seed, byte[] byteptr);

        public unsafe static void Roll32_CSharp(UInt32 seed, byte[] byteptr)
        {
            Random ran = new Random((int)seed);
            //Span<byte> sp = new Span<byte>(byteptr, 32);
            ran.NextBytes(byteptr);
        }
        public unsafe static void Roll32_CSharp2(UInt32 seed, byte[] byteptr)
        {
            Random ran = new Random((int)seed);
            for (var i = 0; i < 8; i++)
            {
                var data = BitConverter.GetBytes(ran.Next());
                byteptr[i * 4 + 0] = data[0];
                byteptr[i * 4 + 1] = data[1];
                byteptr[i * 4 + 2] = data[2];
                byteptr[i * 4 + 3] = data[3];
            }
            //Span<byte> sp = new Span<byte>(byteptr, 32);
        }

        public static void Roll32_Num(UInt32 seed, byte[] byteptr)
        {
            var bb = BitConverter.GetBytes(seed);
            for (var i = 0; i < 32; i++)
            {
                if (i < 4)
                {
                    byteptr[i] = bb[i];
                }
                else
                {
                    byteptr[i] = 0;
                }
            }
        }
    }
}
